/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.provider.PaintPagerAdapter;
import com.cv4j.app.provider.ViewCreateHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;

/**
 * 画风效果
 *
 * @since 2021-05-20
 */
public class PaintAilitySlice extends BaseAbilitySlice implements
    TabList.TabSelectedListener, PageSlider.PageChangedListener {
    private static final int ZERO = 0;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private TabList tabList;
    private PageSlider pageSlider;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_paint;
    }

    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    private void initView() {
        tabList = (TabList) findComponentById(ResourceTable.Id_title_tabs);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);
        tabList.addTabSelectedListener(this);
        pageSlider.addPageChangedListener(this);
        String[] tabsTitle;
        try {
            tabsTitle = getResourceManager().getElement(ResourceTable.Strarray_tab_title).getStringArray();
            for (int i1 = 0; i1 < tabsTitle.length; i1++) {
                TabList.Tab tab = tabList.new Tab(getContext());
                tab.setText(tabsTitle[i1]);
                tabList.addTab(tab);
            }
            tabList.selectTabAt(0);
            pageSlider.setProvider(new PaintPagerAdapter(initPageSliderViewData()));
            pageSlider.setCurrentPage(0);
            pageSlider.setReboundEffect(true);
            pageSlider.setCentralScrollMode(true);
        } catch (NotExistException | IOException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    private ArrayList<Component> initPageSliderViewData() {
        int pageSize = tabList.getTabCount();
        ArrayList<Component> pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper(getContext(), PaintAilitySlice.this);
        for (int i1 = 0; i1 < pageSize; i1++) {
            if (i1 == ZERO) {
                Optional<Component> optionalComponent = viewCreateHelper.createViewHome(tabList.getTabAt(i1).getText());
                if (optionalComponent.isPresent()) {
                    pages.add(optionalComponent.get());
                }
            }
            if (i1 == ONE) {
                Optional<Component> optionalComponent = viewCreateHelper.createViewPage(tabList.getTabAt(i1).getText());
                if (optionalComponent.isPresent()) {
                    pages.add(optionalComponent.get());
                }
            }
            if (i1 == TWO) {
                Optional<Component> optionalComponent =
                    viewCreateHelper.createViewIndicator(tabList.getTabAt(i1).getText());
                if (optionalComponent.isPresent()) {
                    pages.add(optionalComponent.get());
                }
            }
        }
        return pages;
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
    }

    @Override
    public void onUnselected(TabList.Tab tab) {
    }

    @Override
    public void onReselected(TabList.Tab tab) {
    }

    @Override
    public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
    }

    @Override
    public void onPageSlideStateChanged(int state) {
    }

    @Override
    public void onPageChosen(int itemPos) {
        if (tabList.getSelectedTab().getPosition() != itemPos) {
            tabList.selectTabAt(itemPos);
        }
    }
}